﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace 数据表格
{
    public partial class Form1 : Form
    {
        // 定义一个货物数据集
        private DataSet ds = new DataSet();

        public Form1()
        {
            InitializeComponent();
        }

        private void OnFormLoad(object sender, EventArgs e)
        {
            ////定义一个货物数据集
            //DataSet ds = new DataSet();

            //定义日用品表和文具表
            DataTable daily_Necessities = new DataTable();

            //把表添加到数据集合中
            ds.Tables.Add(daily_Necessities);

            //给表定义列
            DataColumn comName = new DataColumn("商品名", typeof(string));
            DataColumn comPrice = new DataColumn("价格", typeof(int));
            DataColumn comInventory = new DataColumn("库存", typeof(int));

            //把列放到表里面
            //*****用数组方式批量添加******
            daily_Necessities.Columns.AddRange(
                new DataColumn[] { comName, comPrice, comInventory }
            );

            //给表中添加数据
            daily_Necessities.Rows.Add("牙膏", 20, 100);
            daily_Necessities.Rows.Add("洗衣液", 18, 67);
            daily_Necessities.Rows.Add("洗衣粉", 18, 67);
            daily_Necessities.Rows.Add("洗手液", 18, 67);


            //设置表格控件的DataSource属性
            dataGridView1.DataSource = ds.Tables[0];
            //设置数据表格上显示的列标题
            dataGridView1.Columns[0].HeaderText = "商品名";
            dataGridView1.Columns[1].HeaderText = "价格";
            dataGridView1.Columns[2].HeaderText = "库存";

            //设置数据表格为只读
            dataGridView1.ReadOnly = true;
            //不允许添加行
            dataGridView1.AllowUserToAddRows = false;
            //背景为白色
            dataGridView1.BackgroundColor = Color.White;
            //只允许选中单行
            dataGridView1.MultiSelect = false;
            //整行选中
            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            ds.Tables[0].Rows.Add("高压锅", 20, 100);
        }
    }



   
}
